package com.javase.annotation;

import java.lang.annotation.*;
import java.lang.reflect.Field;

/**
 * @Auther: liyanhao
 * @Description: 自定义注解
 * @Date: 2022/5/18 12:04
 * @Version: v1.0
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {
    //供应商编号
    public int id() default -1;
    //供应商名称
    public String name() default "";
    //供应商地址
    public String address() default "";
}
//注解的使用
class Apple{
    @FruitProvider(id = 1,name = "陕西富士苹果",address = "陕西省西安延安路")
    private String appleProvider;

    public String getAppleProvider() {
        return appleProvider;
    }

    public void setAppleProvider(String appleProvider) {
        this.appleProvider = appleProvider;
    }
}
//注解处理器
class FruitInfoUtil{
    public static void getFruitInfo(Class<?> clazz) {
       String strFruitProvider = "供应商";
        Field[] declaredFields =
                clazz.getDeclaredFields();//通过反射获取处理注解
        for (Field field : declaredFields) {
            if (field.isAnnotationPresent(FruitProvider.class)) {
                FruitProvider fruitProvider = field.getAnnotation(FruitProvider.class);
                //注解信息的处理地方
                strFruitProvider = "供应商编号:"+fruitProvider.id()+"供应商名称："+
                        fruitProvider.address();
                System.out.println(strFruitProvider);
            }
        }
    }
}
class TestMain{
    public static void main(String[] args) {
        FruitInfoUtil.getFruitInfo(Apple.class);
    }
}
//内部类